package shixun;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;

import org.openqa.selenium.WebElement;

import io.appium.java_client.AppiumBy;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;

public class ChatPage {

	AndroidDriver  driver;
	BaseAction action;
	
	public ChatPage(AndroidDriver  driver) {
		this.driver = driver;
		action= new BaseAction(driver);
	}

//	发送消息内容
	public String str;
	
//	服务器
	public WebElement service() {
		return driver.findElement
				(AppiumBy.xpath("//*[@text='服务器']"));
	}
	
//	账号
	public WebElement username() {
		return driver.findElement
				(AppiumBy.xpath("//*[@text='账号']"));
	}
	
//	密码
	public WebElement password() {
		return driver.findElement
				(AppiumBy.xpath("//*[@text='密码']"));
	}
	
//	点击登录
	public WebElement clickLogin() {
		return driver.findElement
				(AppiumBy.xpath("//android.widget.Button[@content-desc=\"登录\"]"));
	}
	
//	“聊天”界面
	public WebElement chat() {
		return driver.findElement
				(AppiumBy.accessibilityId("聊天\n聊天\n第 3 个标签，共 3 个"));
	}
	
//	点击系统聊天框
	public WebElement system() {
		return driver.findElements
				(AppiumBy.className("android.view.View")).get(3);
	}
	
//	聊天框输入内容
	public WebElement input() {
		return driver.findElement
				(AppiumBy.className("android.widget.EditText"));
	}
	
	public WebElement text() {
	return driver.findElement
			(AppiumBy.xpath("//*[@text='发送消息']"));
}
	
//	发送
	public WebElement send() {
		return driver.findElement
				(AppiumBy.accessibilityId("发送"));
	}
	
//	长按消息
	public WebElement longPress(String str) {
		return driver.findElement
				(AppiumBy.accessibilityId(str));
	}
	
//	点击撤回
	public WebElement drawback() {
		return driver.findElement
				(AppiumBy.accessibilityId("撤回"));
	}
	
//	点击复制
	public WebElement copy() {
		return driver.findElement
				(AppiumBy.accessibilityId("复制"));
	}
	
//	点击粘贴
	public WebElement paste() {
		return driver.findElement
				(AppiumBy.accessibilityId("粘贴"));
	}
	
//	点击转发
	
	public WebElement relay() {
		return driver.findElement
				(AppiumBy.accessibilityId("转发"));
	}
	
	
//	对话框admin
	public WebElement admin() {
		return driver.findElement
				(AppiumBy.accessibilityId("A\nadmin（我）"));
	}
	
//	返回主界面
	public WebElement back() {
		return driver.findElement
				(AppiumBy.accessibilityId("返回"));
	}
	
	
//	聊天界面搜索框
	public WebElement chatSearch() {
		return driver.findElements
				(AppiumBy.className("android.widget.Button")).get(1);
	}
	
//	搜索
	public WebElement search() {
		return driver.findElements
				(AppiumBy.className("android.widget.EditText")).get(0);
	}
	
//	取消搜索
	public WebElement cancelSearch() {
		return driver.findElement
				(AppiumBy.accessibilityId("取消"));
	}
	
//	添加聊天
	public WebElement addChat() {
		return driver.findElement
				(AppiumBy.xpath("//android.widget.Button[@content-desc=\"创建聊天\"]"));
	}
	
//	用户user1
	public WebElement user1() {
		return driver.findElement
				(AppiumBy.accessibilityId("U\nuser1"));
	}
//	用户user2
	public WebElement user2() {
		return driver.findElement
				(AppiumBy.accessibilityId("U\nuser2"));
	}
//	用户user3
	public WebElement user3() {
		return driver.findElement
				(AppiumBy.accessibilityId("U\nuser3"));
	}
	
//	点击创建
	public WebElement buildUsers() {
		return driver.findElement
				(AppiumBy.xpath("//android.widget.Button[@content-desc=\"创建\"]"));
	}
	
//	输入名称
	public WebElement enterName() {
		return driver.findElements
				(AppiumBy.className("android.widget.EditText")).get(0);
	}
	
//	显示菜单
	public WebElement menu() {
		return driver.findElement
				(AppiumBy.xpath("//android.widget.Button[@content-desc=\"显示菜单\"]"));
	}
	
	
//	重命名
	public WebElement renameButton() {
		return driver.findElement
				(AppiumBy.xpath("//android.widget.Button[@content-desc=\"重命名\"]"));
	}
	

	
	
	
//	“通讯录”界面
	public WebElement list() {
		return driver.findElement
				(AppiumBy.accessibilityId("通讯录\n通讯录\n第 2 个标签，共 3 个"));
	}
	
	//	收藏
	public WebElement start() {
		return driver.findElement
				(AppiumBy.accessibilityId("取消收藏"));
	}
	
//	“我的”界面
	public WebElement my() {
		return driver.findElement
				(AppiumBy.accessibilityId("我的\n我的\n第 3 个标签，共 3 个"));
	}
	
//	注销
	public WebElement logout() {
		return driver.findElement
				(AppiumBy.xpath("//android.widget.Button[@content-desc=\"注销\"]"));
	}
	
//	确认
	public WebElement ensure() {
		return driver.findElement
				(AppiumBy.xpath("//android.widget.Button[@content-desc=\"确认\"]"));
	}
	
	
	
	
//	1.登录
	public void login() throws InterruptedException {
		Thread.sleep(2000);
		action.click(this.service());
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_1));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_0));
		driver.pressKey(new KeyEvent(AndroidKey.PERIOD));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_7));
		driver.pressKey(new KeyEvent(AndroidKey.PERIOD));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_9));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_0));
		driver.pressKey(new KeyEvent(AndroidKey.PERIOD));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_1));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_4));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_9));
		Thread.sleep(2000);
		action.click(this.username());
		driver.pressKey(new KeyEvent(AndroidKey.A));
		driver.pressKey(new KeyEvent(AndroidKey.D));
		driver.pressKey(new KeyEvent(AndroidKey.M));
		driver.pressKey(new KeyEvent(AndroidKey.I));
		driver.pressKey(new KeyEvent(AndroidKey.N));
		Thread.sleep(2000);
		action.click(this.password());
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_1));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_2));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_3));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_4));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_5));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_6));
		Thread.sleep(2000);
		action.click(this.clickLogin());
		
//		断言是否存在聊天标识
        assertNotNull(AppiumBy.accessibilityId("聊天\n聊天\n第 1 个标签，共 3 个"));
	}
	
//	2.系统发送内容
	public void systemChat() throws InterruptedException{
		Thread.sleep(5000);
		action.click(this.system());
		Thread.sleep(2000);
		action.click(this.input());
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_1));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_2));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_3));
		Thread.sleep(2000);
		action.click(this.send());
		Thread.sleep(2000);
		
//		断言
		assertNotNull(AppiumBy.xpath("(//android.view.View[@content-desc=\"123\"])"));
	}
	
//	3.撤回系统聊天界面发送的内容
	public void systemDrawback() throws InterruptedException{
		Thread.sleep(2000);
		action.click(this.input());
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_2));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_3));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_4));
		Thread.sleep(2000);
		action.click(this.send());
		Thread.sleep(2000);
		action.longPress(this.longPress("234"));
		Thread.sleep(2000);
		action.click(this.drawback());
		Thread.sleep(2000);
//		断言
		assertNull(AppiumBy.xpath("(//android.view.View[@content-desc=\"copy\"])"));
	}
	
//	4.复制系统聊天界面发送的内容
	public void systemCopy() throws InterruptedException{
		Thread.sleep(2000);
		action.click(this.input());
		Thread.sleep(2000);
		driver.pressKey(new KeyEvent(AndroidKey.C));
		driver.pressKey(new KeyEvent(AndroidKey.O));
		driver.pressKey(new KeyEvent(AndroidKey.P));
		driver.pressKey(new KeyEvent(AndroidKey.Y));
		Thread.sleep(2000);
		action.click(this.send());
		Thread.sleep(2000);
		action.longPress(this.longPress("copy"));
		Thread.sleep(2000);
		action.click(this.copy());
		Thread.sleep(2000);

	}
	
//	5.转发系统聊天界面发送的内容到user1
	public void systemRelay() throws InterruptedException{
		Thread.sleep(2000);
		action.click(this.input());
		Thread.sleep(2000);
		driver.pressKey(new KeyEvent(AndroidKey.R));
		driver.pressKey(new KeyEvent(AndroidKey.E));
		driver.pressKey(new KeyEvent(AndroidKey.L));
		driver.pressKey(new KeyEvent(AndroidKey.A));
		driver.pressKey(new KeyEvent(AndroidKey.Y));
		Thread.sleep(2000);
		action.click(this.send());
		Thread.sleep(2000);
		action.longPress(this.longPress("relay"));
		Thread.sleep(2000);
		action.click(this.relay());
		Thread.sleep(5000);
		action.click(this.admin());
		Thread.sleep(2000);
		action.click(this.back());
		Thread.sleep(2000);
//		断言是否出现已转发内容
		assertNotNull(AppiumBy.accessibilityId("已转发"));
	}
	
//	6.在聊天界面搜索用户user1
	public void searchUser1() throws InterruptedException {
		Thread.sleep(2000);
		action.click(this.chatSearch());
		Thread.sleep(2000);
		action.click(this.search());
		Thread.sleep(2000);
		driver.pressKey(new KeyEvent(AndroidKey.U));
		driver.pressKey(new KeyEvent(AndroidKey.S));
		driver.pressKey(new KeyEvent(AndroidKey.E));
		driver.pressKey(new KeyEvent(AndroidKey.R));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_1));
		Thread.sleep(2000);
		driver.pressKey(new KeyEvent(AndroidKey.ENTER));
		Thread.sleep(2000);
		
//		断言是否出现user1
		assertNotNull(AppiumBy.accessibilityId("U\nuser1"));
		
//		取消，回到主界面
		action.click(this.cancelSearch());
		Thread.sleep(2000);
	}
	
	
//	7.与用户user1，user2，user3，创建聊天群组，名称为users
	public void build() throws InterruptedException{
		Thread.sleep(2000);
		action.click(this.addChat());
		Thread.sleep(2000);
		action.click(this.user1());
		Thread.sleep(2000);
		action.click(this.user2());
		Thread.sleep(2000);
		action.click(this.user3());
		Thread.sleep(2000);
		action.click(this.buildUsers());
		Thread.sleep(2000);
		action.click(this.enterName());
		Thread.sleep(2000);
		driver.pressKey(new KeyEvent(AndroidKey.U));
		driver.pressKey(new KeyEvent(AndroidKey.S));
		driver.pressKey(new KeyEvent(AndroidKey.E));
		driver.pressKey(new KeyEvent(AndroidKey.R));
		driver.pressKey(new KeyEvent(AndroidKey.S));
		Thread.sleep(2000);
		action.click(this.buildUsers());
		Thread.sleep(2000);
		
//		断言是否出现“users”
		assertNotNull(AppiumBy.accessibilityId("users (4)"));
	}
	
	
//	8.将聊天群组users重命名为“users123”
	public void renameUser123() throws InterruptedException{
		Thread.sleep(2000);
		action.click(this.menu());
		Thread.sleep(2000);
		action.click(this.renameButton());
		Thread.sleep(2000);
		action.click(this.enterName());
		Thread.sleep(2000);
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_1));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_2));
		driver.pressKey(new KeyEvent(AndroidKey.DIGIT_3));
		Thread.sleep(2000);
		action.click(this.renameButton());
		Thread.sleep(2000);
//		断言是否出现“用户组123”
		assertNotNull(AppiumBy.accessibilityId("users123 (4)"));
		
		action.click(this.back());
		Thread.sleep(2000);
	}
	
//	9.在通讯录界面收藏用户user1
	public void startUser1() throws InterruptedException{
		Thread.sleep(2000);
		
		action.click(this.list());
		Thread.sleep(2000);
		action.click(this.user1());
		Thread.sleep(2000);
		action.click(this.start());
		Thread.sleep(2000);
		action.click(this.back());
		
		
//		断言是否出现“收藏”
		assertNotNull(AppiumBy.accessibilityId("收藏"));
	}
	
//	10.退出
	public void logoutAdmin() throws InterruptedException{
		Thread.sleep(2000);
		action.click(this.my());
		Thread.sleep(2000);
		action.click(this.logout());
		Thread.sleep(2000);
		action.click(this.ensure());
		Thread.sleep(2000);
		
//		断言是否出现“登录”
		assertNotNull(AppiumBy.accessibilityId("登录"));
	}
	
	
}
